package com.zpark.hystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zpark.hystrix.feign.IUserServerFeign;
import com.zpark.hystrix.pojo.Ranking;
import com.zpark.hystrix.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @PackageName: com.zpark.hystrix.controller
 * @ClassName: Hystrix01Controller
 * @Description:
 * @author: RZS
 * @date: 2022/11/29  10:54
 */
@RestController
@RequestMapping("/hystrix-03")
@DefaultProperties(defaultFallback = "defaultFallback")
public class Hystrix03Controller {

    @Autowired
    private IUserServerFeign userServerFeign;

    @PostMapping("/all.do")
    @HystrixCommand
    public JsonResult all(Integer id){
        return new JsonResult<List<Ranking>>(
                200,
                null,
                userServerFeign.findAll()
        );
    }

    @GetMapping("/find/{id}")
    @HystrixCommand
    public JsonResult find(@PathVariable Integer id){
        return new JsonResult<Ranking>(
                200,
                null,
                userServerFeign.findById(id)
        );
    }

    // 默认服务降级方法
    private JsonResult defaultFallback(){
        return new JsonResult(
                500,
                "服务器繁忙，稍后再试....JSON_RESULT",
                null
        );
    }
}
